package com.arges.web.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.arges.web.common.DateUtil;
import com.arges.web.common.SysEnum.Zlfs;
import com.arges.web.dao.GenericDao;
import com.arges.web.dao.TreatDao;
import com.arges.web.dao.WorkDao;
import com.arges.web.domain.bean.TreatBean;
import com.arges.web.domain.pojo.Pat;
import com.arges.web.domain.pojo.Treat;
import com.arges.web.domain.pojo.Work;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.PatManager;
import com.arges.web.service.TreatManager;

/**
 * 患者诊断治疗业务层
 *
 * @author zxj
 *
 */
@Service
public class TreatManagerImpl extends GenericManageImpl<Treat, TreatBean> implements TreatManager
{
	private Logger LOGGER = LoggerFactory.getLogger(TreatManagerImpl.class);

	@Autowired
	private TreatDao treatDao;

	@Autowired
	private WorkDao workDao;

	@Autowired
	private PatManager patManager;

	@Override
	public GenericDao<Treat, Long> getGenericDao()
	{
		return treatDao;
	}

	@Override
	public PageInfo<TreatBean> getPageTreat(PageInfo<Treat> pageInfo, String mrn, String startTime, String endTime)
	{
		PageInfo<TreatBean> beanPageList = null;
		try
		{
			Criterion condiition = null;
			if (StringUtils.isNotBlank(mrn.trim()))
			{
				condiition = Restrictions.like("mrn", mrn, MatchMode.ANYWHERE);
			}
			if (StringUtils.isNotBlank(startTime) && (StringUtils.isNotBlank(endTime)))
			{
				Date startDate = DateUtil.convertString2Date(startTime, DateUtil.DEFAULT_DATE_TIME);
				Date endDate = DateUtil.convertString2Date(endTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.between("t_date", startDate, endDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			else if (StringUtils.isNotBlank(startTime))
			{
				Date startDate = DateUtil.convertString2Date(startTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.ge("t_date", startDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			else if (StringUtils.isNotBlank(endTime))
			{
				Date endDate = DateUtil.convertString2Date(endTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.le("t_date", endDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			PageInfo<Treat> pojoPageList = treatDao.findAll(pageInfo, Treat.class, condiition);

			beanPageList = WebUtils.pojoToBeanPage(pojoPageList, TreatBean.class);

			List<Work> workList = workDao.findAll();
			List<Pat> patList = patManager.findAll();
			Zlfs[] zlfsList = Zlfs.values();
			Map<Integer, String> zlfsMap = new HashMap<Integer, String>();
			for (Zlfs zlfs : zlfsList)
			{
				zlfsMap.put(zlfs.getId(), zlfs.name());
			}
			for (TreatBean treatBean : beanPageList.getResult())
			{
				for (Pat pat : patList)
				{
					if (pat.getMrn().equals(treatBean.getMrn()))
					{
						treatBean.setP_name(pat.getName());
					}
				}
				for (Work work : workList)
				{
					if (work.getEno().equals(treatBean.getEno()))
					{
						treatBean.setD_name(work.getD_name());
					}
				}
				treatBean.setTherary(zlfsMap.get(Integer.valueOf(treatBean.getTherary())));
			}

		}
		catch (Exception e)
		{
			return null;
		}
		return beanPageList;
	}

	@Override
	public TreatBean findTreatById(long parseLong)
	{
		Treat treat = treatDao.get(parseLong);
		TreatBean treatBean = null;
		if (null != treat)
		{
			treatBean = new TreatBean();
			BeanUtils.copyProperties(treat, treatBean);
		}
		return treatBean;
	}

	@Override
	public void deleteByIds(String ids)
	{
		String[] split = ids.split(",");
		Long[] deleteId = new Long[split.length];
		for (int i = 0; i < split.length; i++)
		{
			deleteId[i] = Long.valueOf(split[i]);
		}
		treatDao.deletePatByIds(deleteId);
	}

	@Override
	public List<Treat> findTreatBymrn(String mrn)
	{
		Criterion condiition = null;
		if (StringUtils.isNotBlank(mrn))
		{
			condiition = Restrictions.eq("mrn", mrn);
			Criterion geCriterion = Restrictions.ge("therary", "1");
			condiition = Restrictions.and(condiition, geCriterion);
			Criterion liCriterion = Restrictions.le("therary", "6");
			condiition = Restrictions.and(condiition, liCriterion);
			List<Treat> treatList = treatDao.getTreatIsTreat(Treat.class, condiition);
			return treatList;
		}
		return null;
	}

}
